#!/bin/bash


function gao() {
	# 安装c++
	if [[ ! -x $(command -v gcc) ]]; then
		yum install gcc gcc-c++ -y
	fi
	# 安装git
	if [[ ! -x $(command -v git) ]]; then
		yum -y install https://packages.endpointdev.com/rhel/7/os/x86_64/endpoint-repo.x86_64.rpm
		yum install -y git
	fi
	if (( $(strings /lib64/libc.so.6 | grep 'GLIBC_2.18' | wc -l) == 0 )); then
		wget http://mirrors.nju.edu.cn/gnu/glibc/glibc-2.18.tar.gz
		(( $? != 0 )) && echo "下载glibc-2.18失败,请手动安装" && return 1
		tar -zxf glibc-2.18.tar.gz
		cd glibc-2.18
		local num_cpu=$(cat /proc/cpuinfo  | grep -c "cpu cores")
		mkdir build && cd build && ../configure --prefix=/usr && make -j${num_cpu} && make install && cd ../../..
		(( $? != 0 )) && echo "安装glibc-2.18失败,请手动安装" && return 1
		rm -rf glibc-2.18 glibc-2.18.tar.gz
	fi

	if [[ ! -x $(command -v python3) ]]; then
		# 安装python3
		echo "开始安装python3"
		yum install -y python3
	fi
	echo "安装python支持"
	pip3 install --upgrade pip
	pip3 install pynvim
	pip3 install pygments
	pip3 install neovim

	# 安装fuse, fuse-devel
	yum install -y fuse fuse-devel unzip

	if [[ ! -x $(command -v node) ]]; then
		# 安装nodejs
		echo "安装nodejs v16.5.0,注意不要太高版本,glibc不兼容"
		rm -rf node-v16.5.0-linux-x64.tar.gz
		wget https://nodejs.org/dist/v16.5.0/node-v16.5.0-linux-x64.tar.gz
		tar -zxf node-v16.5.0-linux-x64.tar.gz && mv node-v16.5.0-linux-x64 /usr/local && cd /usr/local/bin && ln -sf /usr/local/node-v16.5.0-linux-x64/bin/* . && cd -
		(( $? != 0 )) && echo "安装node.js失败" && return 1
		echo "当前目录:$(pwd)"
	fi

	npm config set registry https://mirrors.huaweicloud.com/repository/npm/
	npm install -g neovim
	if [[ ! -x $(command -v yarn) ]]; then
		npm install -g yarn
		(( $? != 0 )) && echo "安装yarn失败,可以考虑手动安装" && return 1
		ln -sf /usr/local/node-v16.5.0-linux-x64/bin/yarn /usr/local/bin/
		echo "安装yarn完成"
	fi

	if [[ ! -x $(command -v clangd) ]]; then
		local latest_version=$(curl -Ls https://github.com/clangd/clangd/releases/latest | grep '<title' | awk '{print $2}')
		if [[ ! -f clangd-linux-${latest_version}.zip ]]; then
			echo "下载最新版本clangd:${latest_version}"
			wget https://github.com/clangd/clangd/releases/download/${latest_version}/clangd-linux-${latest_version}.zip
			(( $? != 0 )) && echo "下载cland失败" && return 1
		fi
		unzip clangd-linux-${latest_version}.zip
		mv clangd_${latest_version} /usr/local/clang
		ln -s /usr/local/clang/bin/clangd /usr/local/bin/clangd
		rm -rf clangd-linux-*
		echo "安装clangd完成"
	fi

	if [ ! -f nvim.appimage ];then
		wget https://github.com/neovim/neovim/releases/latest/download/nvim.appimage
		(( $? != 0 )) && echo "下载neovim失败,请手动安装" && return 1
	fi 
	chmod u+x nvim.appimage
	yum remove vim
	[[ -f /bin/vim ]] && rm /bin/vim
	./nvim.appimage --appimage-extract
	if [[ -d /usr/local/squashfs-root ]]; then
		rm -rf /usr/local/squashfs-root
	fi
	mv squashfs-root /usr/local/
	ln -sf /usr/local/squashfs-root/AppRun /usr/local/bin/vim
	ln -sf usr/local/squashfs-root/AppRun /usr/local/bin/vimdiff
	echo "nvim安装成功"
}

gao